package com.idconfirmsystem.activity;

import java.util.Date;

import org.kymjs.kjframe.KJActivity;
import org.kymjs.kjframe.http.HttpCallBack;
import org.kymjs.kjframe.ui.BindView;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.idconfirmsystem.Conf;
import com.idconfirmsystem.R;
import com.idconfirmsystem.app.widget.MullineLayout;
import com.idconfirmsystem.lib.StringUtil;
import com.idconfirmsystem.model.NoticeUp;
import com.idconfirmsystem.model.UserBean;
import com.idconfirmsystem.remote.ConfirmApi;
import com.idconfirmsystem.util.DateUtil;

public class SendMsgActivity extends KJActivity {

	private MullineLayout mContactsLayout;
	@BindView(id = R.id.contacts_input)
	private EditText mContactsInput;
	@BindView(id = R.id.et_msg_content)
	private EditText mInput;
	@BindView(id = R.id.et_msg_title)
	private EditText mTitle;
	@BindView(id = R.id.btn_send, click = true)
	private Button btnSend;
	@BindView(id = R.id.btn_cancel, click = true)
	private Button btnCancel;
	@BindView(id = R.id.contacts, click = true)
	private Button btnAddContact;
	@BindView(id = R.id.et_msg_remark)
	private EditText mRemark;

	private boolean isEditEmpty = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public void setRootView() {
		// TODO Auto-generated method stub
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_send_msg);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.contacts:// 添加联系人
			addContacts(mContactsLayout.getChildCount() + "zhang");
			break;
		case R.id.btn_cancel:// 取消
			finish();
			break;
		case R.id.btn_send:// 发送消息
			if (StringUtil.isEmpty(mTitle.getText().toString())) {
				toast("请输入标题");
				mTitle.requestFocus();
				return;
			}
			if (StringUtil.isEmpty(mInput.getText().toString())) {
				toast("请输入消息内容");
				mInput.requestFocus();
				return;
			}
			NoticeUp upobj = getForm();
			ConfirmApi.pushData(upobj, "NoticeService/addNotice", callBack);
			break;
		default:
			break;
		}
	}

	HttpCallBack callBack = new HttpCallBack() {

		@Override
		public void onFailure(int errorNo, String strMsg) {
			// TODO Auto-generated method stub
			super.onFailure(errorNo, strMsg);
			toast("发送消息失败:" + strMsg);
		}

		@Override
		public void onSuccess(String t) {
			// TODO Auto-generated method stub
			super.onSuccess(t);
			toast("发送成功");
			SendMsgActivity.this.setResult(Conf.ResultCode.SEND_MSG_BACK);
			SendMsgActivity.this.finish();
		}

	};

	private void toast(String msg) {
		Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
	}

	private NoticeUp getForm() {
		NoticeUp noticeUp = new NoticeUp();
		noticeUp.setContent(mInput.getText().toString());
		noticeUp.setTitle(mTitle.getText().toString());
		noticeUp.setDate(DateUtil.getDateString(new Date(), DateUtil.DATE_FORMAT_NORMAL_SECOND_no_space));
		noticeUp.setDate(noticeUp.getDate());
		noticeUp.setLoginUserId(UserBean.getInstance().userName);
		noticeUp.setSourceType(Conf.LoginConf.ACCOUNT_TYPE);
		noticeUp.setSourceNo(Conf.LoginConf.ACCOUNT_TYPE == 1 ? "shouya" : UserBean.getInstance().branchNumber);
		noticeUp.setRemark(mRemark.getText().toString());
		return noticeUp;
	}

	@Override
	public void initWidget() {
		// TODO Auto-generated method stub
		super.initWidget();
		mContactsLayout = (MullineLayout) findViewById(R.id.contacts_layout);
		mContactsLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mContactsInput.setFocusable(true);
				mContactsInput.setFocusableInTouchMode(true);
				mContactsInput.requestFocus();
			}
		});
	}

	private void addContacts(String name) {
		mContactsLayout.addView(initContactsItemView(name), mContactsLayout.getChildCount() - 1);
	}

	@SuppressLint("InflateParams")
	private View initContactsItemView(String name) {
		final View view = LayoutInflater.from(this).inflate(R.layout.item_contacts, null);
		Button contactButton = (Button) view.findViewById(R.id.btn_contact);
		contactButton.setText(name);
		contactButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mContactsLayout.removeView(view);
			}
		});
		return view;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_DEL && isEditEmpty && mContactsInput.getText().toString().length() <= 0
				&& mContactsLayout.getChildCount() > 1) {
			mContactsLayout.removeViewAt(mContactsLayout.getChildCount() - 2);
			return true;
		}
		if (mContactsInput.getText().toString().length() <= 0) {
			isEditEmpty = true;
		} else {
			isEditEmpty = false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
